/**
 * 
 */
package edu.umd.clip.lm.playground;

import java.io.*;
import org.w3c.dom.*;
import org.w3c.dom.ls.*;
import org.w3c.dom.bootstrap.*;
import org.xml.sax.*;

import javax.xml.parsers.*;
import edu.umd.clip.lm.questions.*;
import edu.umd.clip.lm.util.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class XMLQuestions {
	private static Document doc;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
			DOMImplementationLS impl = 
			    (DOMImplementationLS)registry.getDOMImplementation("LS");
			LSSerializer writer = impl.createLSSerializer();
			writer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.newDocument();
			
			Question question1 = new BinaryPrefixQuestion((byte)-1, true, new BinaryPrefix(0).appendOne().appendZero().appendOne().appendZero());
			Question question2 = new IdentityQuestion((byte)-2, (byte)1, 15);
			
			System.out.printf("Question1: %s\n", question1.toString());
			System.out.printf("Question2: %s\n", question2.toString());
			
			// serialize questions
			String serializedQuestion1 = writer.writeToString(question1.createXML(doc));
			String serializedQuestion2 = writer.writeToString(question2.createXML(doc));
			
			System.out.println("Question1 XML: "+serializedQuestion1);
			System.out.println("Question2 XML: "+serializedQuestion2);
			
			// unserialized questions
			Question q1 = Question.fromXML(db.parse(new InputSource(new StringReader(serializedQuestion1))).getDocumentElement());
			Question q2 = Question.fromXML(db.parse(new InputSource(new StringReader(serializedQuestion2))).getDocumentElement());

			System.out.printf("Question1: %s\n", q1.toString());
			System.out.printf("Question2: %s\n", q2.toString());
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}
